<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="菜单编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="title" label="菜单名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标"> </el-table-column>
      <el-table-column prop="url" label="菜单地址"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success"
            >启用</el-button
          >
          <el-button v-else type="danger"> 禁用 </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { menuList, delmenuList } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  data() {
    return {
      tableData: [], //菜单列表
    };
  },
  methods: {
    requestMenu() {
      menuList({ istree: true }).then((res) => {
        if (res.code === 200) {
          this.tableData = res.list;
        }
      });
    },
    del(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          delmenuList({id}).then((res) => {
            if (res.code === 200) {
              successAlert(res.msg);
              this.requestMenu();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    edit(id){
        this.$emit("edit",id);
    }
  },
  mounted() {
    this.requestMenu();
    this.event.$on("message", (e) => {
      this.requestMenu();
    });
  },
};
</script>

<style>
</style>